import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

public class DAL{
    //增
    public  static  void  addstudent () throws IOException {
        resident sc =new resident();
        String sge,name,sid;
        int age;
        BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("请输入门号");
            sid=buf.readLine();
            if(sid.length()==4){
                break;
            }else { System.out.println("门号格式不对，请输入正确格式");}
        }
        String str;
        System.out.println("请输入户主姓名");
        name=buf.readLine();
        System.out.println("请输入户主年龄");
        sge=buf.readLine();
        age=Integer.parseInt(sge);
        sc.setName(name);
        sc.setSid(sid);
        sc.setAge(age);
        String str233;
        boolean a=true;
        str233="门号:"+sc.sid+",户主姓名:"+sc.name+",户主年龄:"+sc.age+"\n";
        BufferedWriter buf233=new BufferedWriter(new FileWriter("warehouse.txt",a));
        BufferedReader in =new BufferedReader(new FileReader("warehouse.txt"));
        while(( str=in.readLine())!=null){
            String syyy=str.substring(3,7);
            if((sc.sid).equals(syyy)){
                System.out.println("门号重复，请重新输入");
                return;
            }
            else{}}
        in.close();
        buf233.write(str233);
        System.out.println("添加成功");
        buf233.close();
    }
    //删
    public  static  void  deletestudent (ArrayList<String> ss) throws IOException {
        String str;
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入门号");
        str=buf.readLine();
        String str6;
        int num;
        try(BufferedReader in =new BufferedReader(new FileReader("warehouse.txt"))){
           for( num=0;(str6=in.readLine())!=null;num++){
                    ss.add(str6);
               String syyy=str6.substring(3,7);
               if(str.equals(syyy)){
                   ss.remove(num);
                   System.out.println("删除成功");
               }
            }
            ss.trimToSize();
            in.close();
            BufferedWriter out =new BufferedWriter(new FileWriter("warehouse.txt"));
            for (int i=0;i<ss.size();i++){
                String str3=ss.get(i);
                out.write(str3);
                out.newLine();
            }
            out.close();
            //迭代清空集合
            Iterator ss1=ss.iterator();
            while (ss1.hasNext()) {
                ss1.next();
                ss1.remove();
            }
        }
        catch(IOException ioe){
        }
    }
    //改
    public  static  void  updatestudent(ArrayList<String> ss) throws IOException {
        String str;
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入门号");
        str=buf.readLine();
        String str6;
        try(BufferedReader in =new BufferedReader(new FileReader("warehouse.txt"))   ){
            for (int num=0;(str6=in.readLine())!=null;num++){
                ss.add(str6);
                String syyy=str6.substring(3,7);
                if(str.equals(syyy)){
                    BufferedReader buf12 =new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("请输入户主姓名");
                    String name1=buf12.readLine();
                    System.out.println("请输入户主年龄");
                    String sge1=buf12.readLine();
                    int age1;
                    age1=Integer.parseInt(sge1);
                    str6="门号:"+str+",户主姓名:"+name1+",户主年龄:"+age1;
                    ss.set(num,str6);
                    System.out.println("修改成功");
                }
            }
            in.close();
            BufferedWriter out =new BufferedWriter(new FileWriter("warehouse.txt"));
            for (int i=0;i<ss.size();i++){
                String str3=ss.get(i);
                out.write(str3);
                out.newLine();
            }
            out.close();
            Iterator ss1=ss.iterator();
            while (ss1.hasNext()) {
                ss1.next();
                ss1.remove();
            }
        }
        catch(IOException ioe){
        }
    }
    //1查
    public  static  void  bianlistudent1() throws IOException {
        BufferedReader buf223 =new BufferedReader(new FileReader("warehouse.txt"));
        String str233;
        String syyy;
        while ((str233=buf223.readLine())!=null){
            String syy=str233.substring(3,7);
            syyy=str233.substring(8,9);
            if ( syyy==null){
                System.out.println("门号"+syy+",无人居住");
            }
            {System.out.println("门号"+syy+",有人居住");}
        }
        buf223.close();
    }
    public  static  void  bianlistudent2() throws IOException {
        BufferedReader buf223 =new BufferedReader(new FileReader("warehouse.txt"));
        String str233;
        while ((str233=buf223.readLine())!=null){
            System.out.println(str233);
        }
        buf223.close();
    }
}
